package com.ks.cms.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;


import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ks.cms.controller.dto.BCmsNewsInfo;
import com.ks.cms.controller.dto.SearchCriteriaBase;

import com.ks.cms.entity.BCmsNews;
import com.ks.cms.dao.BCmsNewsMapper;
import com.ks.cms.service.IBCmsNewsService;

import com.ks.core.base.OperationContext;
import com.ks.core.constant.Const;
import com.ks.core.exception.BizExceptionEnum;
import com.ks.core.exception.GunsException;

import com.ks.core.util.ToolUtil;
import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Service;

import com.ks.core.dto.OperateBase;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 信息发布系统的新闻公告信息表 服务实现类
 * </p>
 *
 * @author kingdee123
 * @since 2020-12-21
 */
@Service
public class BCmsNewsServiceImpl extends ServiceImpl<BCmsNewsMapper, BCmsNews> implements IBCmsNewsService {
    @Resource
    private BCmsNewsMapper bCmsNewsMapper;


    @Override
    public int addBCmsNews(OperationContext context, BCmsNews bCmsNews) {
        bCmsNews.setTenantID(context.getTenantId().intValue());
        bCmsNews.setOrgID(context.getOrgId().intValue());
        bCmsNews.setOrgSubID(context.getOrgSubId().intValue());
        bCmsNews.setCreateUserID(context.getUserId().intValue());
        bCmsNews.setDataType("TENANT");
        if (bCmsNews.getIsVisbile() != null && bCmsNews.getIsVisbile() == 1) {
            if (bCmsNews.getShowModule() < 1) {
                throw new GunsException("必须选择一个位置进行显示!");
            }
        }
        bCmsNewsMapper.setUTF();
        int i = bCmsNewsMapper.insert(bCmsNews);
        return i;
    }

    @Override
    public int deleteBCmsNews(OperationContext context, List<Integer> ids) {
        int count = 0;
        if (ToolUtil.isNotEmpty(ids)) {
            for (int i = 0; i < ids.size(); i++) {
                BCmsNews cmsNews = bCmsNewsMapper.selectById(ids.get(i));
                cmsNews.setDelMark(Const.DEL_FLAG_DEL);
                Integer integer = bCmsNewsMapper.updateById(cmsNews);
                count = +integer;
            }
        }
        return count;
    }


    @Override
    public int updateBCmsNews(OperationContext context, BCmsNews bCmsNews) {

        BCmsNews bCmsNewss = bCmsNewsMapper.selectById(bCmsNews.getId());
        if (ToolUtil.isEmpty(bCmsNewss)) {
            throw new GunsException("该数据不存在");
        }
        bCmsNewsMapper.setUTF();
        Integer i = bCmsNewsMapper.updateById(bCmsNews);
        return i;
    }

    @Override
    public BCmsNews selectInfoBCmsNews(OperationContext context, Integer id) {
        BCmsNews cmsNews = new BCmsNews();
        cmsNews.setTenantID(context.getTenantId().intValue());
        cmsNews.setDelMark(Const.DEL_FLAG_NORMAL);
        cmsNews.setCState(Const.DEL_FLAG_DEL);
        cmsNews.setId(id);
        BCmsNews bCmsNews = bCmsNewsMapper.selectOne(cmsNews);
        if (ToolUtil.isNotEmpty(bCmsNews)) {
            bCmsNews.setIViewCount(bCmsNews.getIViewCount() + 1);
            bCmsNewsMapper.setUTF();
            bCmsNewsMapper.updateById(bCmsNews);
        }
        return bCmsNews;

    }

    @Override
    public Map<String, Object> selectBCmsNews(OperationContext context, SearchCriteriaBase searchCriteriaBase) {
        ArrayList<SearchCriteriaBase.matchField> fields = searchCriteriaBase.getFields();
        EntityWrapper<BCmsNews> wrapper = new EntityWrapper<>();
        BCmsNews bCmsNews = new BCmsNews();
        if (ToolUtil.isNotEmpty(fields)) {
            for (SearchCriteriaBase.matchField field : fields) {
                if ("iTitle".equals(field.getName())) {
                    bCmsNews.setITitle(field.getValue());
                }
                if ("iKeyWords".equals(field.getName())) {
                    bCmsNews.setIKeyWords(field.getValue());
                }
                if ("cType".equals(field.getName())) {
                    bCmsNews.setCType(field.getValue());
                }
            }
        }

        bCmsNews.setTenantID(context.getTenantId().intValue());
        bCmsNews.setDelMark(Const.DEL_FLAG_NORMAL);
        ArrayList<SearchCriteriaBase.StateField> states = searchCriteriaBase.getStates();
        if (ToolUtil.isNotEmpty(states)) {
            for (SearchCriteriaBase.StateField state : states) {
                if ("cState".equals(state.getName())) {
                    bCmsNews.setCState(Integer.valueOf(state.getValue()));
                }
            }
        }
        wrapper.setEntity(bCmsNews);
        if (ToolUtil.isNotEmpty(searchCriteriaBase.getStartTime())) {
            wrapper.and("DATE_FORMAT( createTime, '%Y-%m-%d' ) >=" + "'" + searchCriteriaBase.getStartTime() + "'");
            wrapper.and("DATE_FORMAT( createTime, '%Y-%m-%d' ) <=  " + "'" + searchCriteriaBase.getEndTime() + "'");
        }
        RowBounds limit = new Page(searchCriteriaBase.getPageIndex(), searchCriteriaBase.getLimit());
        List<BCmsNews> bCmsNewsList = bCmsNewsMapper.selectPage(limit, wrapper);
        Map<String, Object> resultMap = this.isResultMap(bCmsNewsList, searchCriteriaBase);
        return resultMap;
    }

    @Override
    public Map<String, Object> selectAdminBCmsNews(OperationContext context, SearchCriteriaBase searchCriteriaBase) {
        ArrayList<SearchCriteriaBase.matchField> fields = searchCriteriaBase.getFields();
        Map<String, Object> map = new HashMap<>();
        EntityWrapper<BCmsNews> wrapper = new EntityWrapper<>();
        BCmsNews bCmsNews = new BCmsNews();
        if (ToolUtil.isNotEmpty(fields)) {
            for (SearchCriteriaBase.matchField field : fields) {
                if ("iTitle".equals(field.getName())) {
                    bCmsNews.setITitle(field.getValue());
                }
                if ("iKeyWords".equals(field.getName())) {
                    bCmsNews.setIKeyWords(field.getValue());
                }
                if ("cType".equals(field.getName())) {
                    bCmsNews.setCType(field.getValue());
                }
                if ("tenantID".equals(field.getName())) {
                    bCmsNews.setTenantID(Integer.valueOf(field.getValue()));
                }
            }
        }

        bCmsNews.setDelMark(Const.DEL_FLAG_NORMAL);
        ArrayList<SearchCriteriaBase.StateField> states = searchCriteriaBase.getStates();
        if (ToolUtil.isNotEmpty(states)) {
            for (SearchCriteriaBase.StateField state : states) {
                if ("cState".equals(state.getName())) {
                    bCmsNews.setCState(Integer.valueOf(state.getValue()));
                }
            }
        }
        wrapper.setEntity(bCmsNews);
        if (ToolUtil.isNotEmpty(searchCriteriaBase.getStartTime())) {
            wrapper.and("DATE_FORMAT( createTime, '%Y-%m-%d' ) >=" + "'" + searchCriteriaBase.getStartTime() + "'");
            wrapper.and("DATE_FORMAT( createTime, '%Y-%m-%d' ) <=  " + "'" + searchCriteriaBase.getEndTime() + "'");
        }
        Integer integer = bCmsNewsMapper.selectCount(wrapper);
        map.put("count",integer);
        RowBounds limit = new Page(searchCriteriaBase.getPageIndex(), searchCriteriaBase.getLimit());
        List<BCmsNews> bCmsNewsList = bCmsNewsMapper.selectPage(limit, wrapper);
        map.put("limit", searchCriteriaBase.getLimit());
        map.put("pageIndex", searchCriteriaBase.getPageIndex());
        map.put("hasMore", false);
        map.put("items", bCmsNewsList);
        return map;
    }

    @Override
    public Map<String, Object> selectBCmsNewsList(OperationContext context, SearchCriteriaBase searchCriteriaBase) {
        Map<String, Object> map = new HashMap<>();
        EntityWrapper<BCmsNews> wrapper = new EntityWrapper<>();
        BCmsNews bCmsNews = new BCmsNews();
        bCmsNews.setTenantID(context.getTenantId().intValue());
        bCmsNews.setDelMark(Const.DEL_FLAG_NORMAL);
        bCmsNews.setCState(Const.DEL_FLAG_DEL);
        wrapper.setEntity(bCmsNews);
        Integer integer = bCmsNewsMapper.selectCount(wrapper);
        map.put("count", integer);
        RowBounds limit = new Page(searchCriteriaBase.getPageIndex(), searchCriteriaBase.getLimit());
        List<BCmsNews> bCmsNewsList = bCmsNewsMapper.selectPage(limit, wrapper);
        map.put("list", bCmsNewsList);
        return map;
    }

    @Override
    public List<BCmsNews> getBCmsNewsList(OperationContext context) {
        EntityWrapper<BCmsNews> wrapper = new EntityWrapper<>();
        BCmsNews bCmsNews = new BCmsNews();
        bCmsNews.setTenantID(context.getTenantId().intValue());
        bCmsNews.setDelMark(Const.DEL_FLAG_NORMAL);
        bCmsNews.setCState(Const.DEL_FLAG_DEL);
        bCmsNews.setShowModule(Const.DEL_FLAG_DEL);
        wrapper.setEntity(bCmsNews);
        List<BCmsNews> cmsNews = bCmsNewsMapper.selectList(wrapper);
        return cmsNews;
    }


    @Override
    public int changeBCmsNewsState(OperationContext context, Integer id, Integer state) {
        BCmsNews bCmsNews = bCmsNewsMapper.selectById(id);
        if (ToolUtil.isNotEmpty(bCmsNews)) {
            bCmsNews.setCState(state);
            bCmsNews.setIEditorName(context.getUserId().toString());
            Integer integer = bCmsNewsMapper.updateById(bCmsNews);
            return integer;
        }
        throw new GunsException("新闻id错误！！！");

    }

    @Override
    public int addCount(int id, int type) {
        BCmsNews cmsNews = bCmsNewsMapper.selectById(id);
        if (ToolUtil.isNotEmpty(cmsNews)) {
            if (type == Const.Cms.COMMENT_TYPE) {
                cmsNews.setICommentCount(cmsNews.getICommentCount() + 1);
                Integer integer = bCmsNewsMapper.updateById(cmsNews);
                return integer;
            } else if (type == Const.Cms.AGREE_TYPE) {
                cmsNews.setIAgreeCount(cmsNews.getIAgreeCount() + 1);
                Integer integer = bCmsNewsMapper.updateById(cmsNews);
                return integer;
            } else if (type == Const.Cms.VIEW_TYPE) {
                cmsNews.setIViewCount(cmsNews.getIViewCount() + 1);
                Integer integer = bCmsNewsMapper.updateById(cmsNews);
                return integer;
            } else if (type == Const.Cms.SHARE_TYPE) {
                cmsNews.setIShareCount(cmsNews.getIShareCount() + 1);
                Integer integer = bCmsNewsMapper.updateById(cmsNews);
                return integer;
            }
        }
        return 0;
    }


    public Map<String, Object> isResultMap(List list, SearchCriteriaBase searchCriteriaBase) {
        Map<String, Object> map = new HashMap<>();
        map.put("count", list.size());
        map.put("limit", searchCriteriaBase.getLimit());
        map.put("pageIndex", searchCriteriaBase.getPageIndex());
        map.put("hasMore", false);
        map.put("items", list);
        return map;
    }
}